package great.easychat.help.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import great.easychat.help.R;


/**
 * 升级提示框
 */
public class UpdateDialog {
    private AlertDialog dialog = null;

    /**
     * @param context  上下文
     * @param callback 回调
     */
    Activity activity;
    ProgressBar pbProgress;
    TextView tvProcess;

    public void show(Activity context) {
        if (dialog == null) {
            activity = context;
            dialog = new AlertDialog.Builder(context).create();
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            dialog.setCancelable(false);//屏蔽返回按钮
            if (!context.isFinishing()) {
                dialog.show();
            }
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setAttributes(lp);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
            window.setContentView(R.layout.update_dialog);
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            tvProcess = window.findViewById(R.id.tvProcess);
            pbProgress = window.findViewById(R.id.pbProgress);
        }
    }

    public void setProgress(int progress) {
        pbProgress.setProgress(progress);
        tvProcess.setText(progress + "%");
    }

    /**
     * 取消窗口
     */
    public void cancelDialog() {
        try {
            if (dialog != null) {
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

}
